﻿using ClusterAware;
using ClusterAwareAdmin;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClusterAwareProperties;

namespace ClusterAwareTest
{
    
    
    /// <summary>
    ///This is a test class for NetworkMethodsTest and is intended
    ///to contain all NetworkMethodsTest Unit Tests
    ///</summary>
    [TestClass]
    public class NetworkMethodsTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        private static ClusterManager _manager;


        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void ClassTestInitialize(TestContext testContext)
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (!managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Create(SharedConsts.NewGroupTest);
            }
            if (!managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Create(SharedConsts.GroupTest);
            }
            if (managerInit.OperationSystem == WindowsOperationSystem.Windows2008 || managerInit.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                var node = managerInit.Groups.GetActiveNode(SharedConsts.GroupTest).Name;
                managerInit.Groups.SetPreferredOwners(SharedConsts.GroupTest, new[] { node });
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceDep, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceDep);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceTest);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
                managerInit.Resources.AddDependency(SharedConsts.ResourceTest, SharedConsts.ResourceDep);
            }
            managerInit.Dispose();
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void ClassTestCleanup()
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceDep);
            }
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.NewGroupTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.GroupTest);
            }
            managerInit.Dispose();
        }
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize]
        public void Initialize()
        {
            _manager = new ClusterManager
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            _manager.Connect();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void Cleanup()
        {
            _manager.Dispose();
        }
        //
        #endregion

        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod]
        public void SaveTest()
        {
            _manager.CacheInstances = true;
            var target = new NetworkMethods(_manager);
            foreach (var net in target.GetNetworks())
            {
                var olddes = net.Description;
                net.Description = "TestSave";
                var actual = target.Save(net);
                Assert.AreEqual("TestSave", net.Description);
                actual.Description = olddes;
                actual = target.Save(actual);
                Assert.AreEqual(olddes, actual.Description);
            }
            _manager.CacheInstances = false;
            foreach (var net in target.GetNetworks())
            {
                var olddes = net.Description;
                net.Description = "TestSave";
                var actual = target.Save(net);
                Assert.AreEqual("TestSave", net.Description);
                actual.Description = olddes;
                actual = target.Save(actual);
                Assert.AreEqual(olddes, actual.Description);
            }
        }

        /// <summary>
        ///A test for GetNetworks
        ///</summary>
        [TestMethod]
        public void GetNetworksTest()
        {
            _manager.CacheInstances = true;
            var target = new NetworkMethods(_manager);
            var actual = target.GetNetworks();
            Assert.AreNotEqual(0, actual.Length);
            _manager.CacheInstances = false;
            actual = target.GetNetworks();
            Assert.AreNotEqual(0, actual.Length);
        }

        /// <summary>
        ///A test for GetNetwork
        ///</summary>
        [TestMethod]
        public void GetNetworkTest()
        {
            _manager.CacheInstances = true;
            var target = new NetworkMethods(_manager);
            foreach (var net in target.GetNetworks())
            {
                var actual = target.GetNetwork(net.Name);
                Assert.AreEqual(net.Name, actual.Name);
            }
            _manager.CacheInstances = false;
            foreach (var net in target.GetNetworks())
            {
                var actual = target.GetNetwork(net.Name);
                Assert.AreEqual(net.Name, actual.Name);
            }
        }

        /// <summary>
        ///A test for ExistNetwork
        ///</summary>
        [TestMethod]
        public void ExistNetworkTest()
        {
            _manager.CacheInstances = true;
            var target = new NetworkMethods(_manager);
            foreach (var net in target.GetNetworks())
            {
                var actual = target.ExistNetwork(net.Name);
                Assert.AreEqual(true, actual);
            }
            var result = target.ExistNetwork("Notfound");
            Assert.AreEqual(false, result);
            _manager.CacheInstances = false;
            foreach (var net in target.GetNetworks())
            {
                var actual = target.ExistNetwork(net.Name);
                Assert.AreEqual(true, actual);
            }
            result = target.ExistNetwork("Notfound");
            Assert.AreEqual(false, result);
        }

        /// <summary>
        ///A test for NetworkMethods Constructor
        ///</summary>
        [TestMethod]
        public void NetworkMethodsConstructorTest()
        {
            _manager.CacheInstances = true;
            var target = new NetworkMethods(_manager);
            Assert.AreNotEqual(null, target);
            _manager.CacheInstances = false;
            target = new NetworkMethods(_manager);
            Assert.AreNotEqual(null, target);
        }
    }
}
